package leetCode.firstWeek;

import java.util.ArrayList;
import java.util.List;

public class Code_20 {
  public boolean isValid(String s) {
    List<Character> stack = new ArrayList<>();
    for (int i = 0; i < s.length(); i++) {
      stack.add(s.charAt(i));
      if (stack.size() >= 2 && isPair(stack.get(stack.size() - 2), (stack.get(stack.size() - 1)))) {
        stack.remove(stack.size() - 1);
        stack.remove(stack.size() - 1);
      }
    }
    if (stack.size() == 0) {
      return true;
    }
    return false;
  }

  public boolean isPair(Character a, Character b) {
    if ((a.equals('(') && b.equals(')'))
        | (a.equals('{') && b.equals('}'))
        | (a.equals('[') && b.equals(']'))) {
      return true;
    }
    return false;
  }
}
